<?php
namespace Managers;

use Catalog\Categories;

class Users extends BaseManager
{
    public function IsAdmin($login, $pas)
    {
        $result = $this->_em->createQueryBuilder()
                ->select('u')
                ->from('\Catalog\User', 'u')
                ->where('u.login = :login')->setParameter('login', $login)
                ->andWhere('u.password = :password')->setParameter('password', md5($pas))
                ->andWhere('u.admin = :admin')->setParameter('admin', 1)
                ->getQuery()
                ->getArrayResult();

        if (count($result) == 0) return false;
        else return $result[0];
    }

    public function MailSpamList()
    {
        $result = $this->_em->createQueryBuilder()
                ->select('ms')
                ->from('\Catalog\Mailspam', 'ms')
                ->getQuery()
                ->getArrayResult();
        return $result;
    }

    public function IPSpamList()
    {
        $result = $this->_em->createQueryBuilder()
                ->select('ips')
                ->from('\Catalog\Ipspam', 'ips')
                ->getQuery()
                ->getArrayResult();
        return $result;
    }

    public function AddMailSpamer($mail)
    {
        $Spam_mail = new \Catalog\Mailspam();
        $Spam_mail->setMail($mail);

        $this->_em->persist($Spam_mail); //Чтобы вставить надо эти 2 штуки
        $this->_em->flush(); //
    }

    public function AddIPspamer($ip)
    {
        $SpamIP = new \Catalog\Ipspam();
        $SpamIP->setIp($ip);

        $this->_em->persist($SpamIP); //Чтобы вставить надо эти 2 штуки
        $this->_em->flush(); //
    }

    public function DeleteIpSpamer($id)
    {
        $entity = $this->_em->find('\Catalog\Ipspam', $id); //достало по айпишке то что будем менять
        if ($entity) {
            $this->_em->remove($entity);
            $this->_em->flush();
        }
    }

    public function DeleteMailSpamer($id)
    {
        $entity = $this->_em->find('\Catalog\Mailspam', $id); //достало по айпишке то что будем менять
        if ($entity) {
            $this->_em->remove($entity);
            $this->_em->flush();
        }
    }

    public function IsMailSpamer($mail)
    {
        $result = $this->_em->createQueryBuilder()
                ->select('ms')
                ->from('\Catalog\Mailspam', 'ms')
                ->where('ms.mail = :mail')->setParameter('mail', $mail)
                ->getQuery()
                ->getArrayResult();
        return $result;

    }

    public function IsIpSpamer($ip)
    {
        $result = $this->_em->createQueryBuilder()
                ->select('ips')
                ->from('\Catalog\Ipspam', 'ips')
                ->where('ips.ip = :ip')->setParameter('ip', $ip)
                ->getQuery()
                ->getArrayResult();
        return $result;
    }

    public function IsRegistered($log, $pas)
    {

        $result = $this->_em->createQueryBuilder()
                ->select('usr')
                ->from('\Catalog\User', 'usr')
                ->where('usr.email = :log')
                ->andWhere('usr.password= :pas')
                ->setParameter('pas', md5($pas))
                ->setParameter('log', $log)

                ->getQuery()
                ->getArrayResult();

        if ($result) {
            return $result;
        }
        else return false;

    }

    public function reguser($email, $pas)
    {
        $user = new \Catalog\User();
        $user->setEmail($email);
        $user->setPassword(md5($pas));
        $user->setAdmin("0");
        $user->setLogin("none");
        $user->setName("none");
        $user->setIsDel(0);

        $this->_em->persist($user);
        $this->_em->flush();


    }


    public function  infoShow($user_id)

    {

        $res = $this->_em->createQueryBuilder()

                ->select('u', 'i')
                ->from('\Catalog\Items', 'i')
                ->join('i.user', 'u')
                ->where('i.user = :id')
                ->setParameter('id', $user_id)
                ->getQuery()
                ->getArrayResult();

        if ($res) {
            return $res;
        }
        else return false;


    }

    public function getItems($user_id)
    {

        $res = $this->_em->createQueryBuilder()

                ->select('i')
                ->from('\Catalog\Items', 'i')
                ->where('i.user = :id')
                ->setParameter('id', $user_id)
                ->getQuery()
                ->getArrayResult();
        return $res;

    }

    public function  getFirstIn($user_id)
    {

        $res = $this->_em->createQueryBuilder()

                ->select('u.firstIn')
                ->from('\Catalog\User', 'u')
                ->where('u.id = :id')
                ->setParameter('id', $user_id)
                ->getQuery()
                ->getArrayResult();

        return $res[0]['firstIn'];


    }

    public function firstin($id)
    {
        $user = $this->_em->find('\Catalog\User', $id);
        $user->setFirstIn(0);
        $this->_em->persist($user);
        $this->_em->flush();


    }

    public function isok($id, $pas)
    {
        $result = $this->_em->createQueryBuilder()
                ->select('usr')
                ->from('\Catalog\User', 'usr')
                ->where('usr.id = :id')
                ->andWhere('usr.password= :pas')
                ->setParameter('pas', md5($pas))
                ->setParameter('id', $id)

                ->getQuery()
                ->getArrayResult();

        if ($result) {
            return "inline";
        }
        else    return "none";

    }
    public function setPas($id,$pas){
           
        $user = $this->_em->find('\Catalog\User', $id);

        if($user){$user->setPassword(md5($pas));

        $this->_em->persist($user);
        $this->_em->flush();}
    }
    public  function updateInfo($id,$phone,$address,$name,$login){

          $user = $this->_em->find('\Catalog\User', $id);

        $user->setName($name);
        $user->setTelephone($phone);
        $user->setAddress($address);
        $user->setLogin($login);
        
        $this->_em->persist($user);
        $this->_em->flush();



    }
    public  function getIdByEmail($email){
         $result = $this->_em->createQueryBuilder()
                ->select('usr')
                ->from('\Catalog\User', 'usr')
                ->where('usr.email = :email')
                ->setParameter('email', $email)

                ->getQuery()
                ->getArrayResult();

     
        if ($result) {
            return $result[0]["id"];
        }
        else    return false;




    }


}